home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Freeware / Programare / highlight / highlight-W32GUI-2.2-10b-Setup.exe / {app} / src / htmlgenerator.cpp < prev    next >
C/C++ Source or Header  |  2005-03-31  |  9KB  |  293 lines

  1. /***************************************************************************
  2.                           htmlcode.cpp  -  description
  3.                              -------------------
  4.     begin                : Wed Nov 28 2001
  5.     copyright            : (C) 2001 by AndrΘ Simon
  6.     email                : andre.simon1@gmx.de
  7.  ***************************************************************************/
  8.  
  9. /***************************************************************************
  10.  *                                                                         *
  11.  *   This program is free software; you can redistribute it and/or modify  *
  12.  *   it under the terms of the GNU General Public License as published by  *
  13.  *   the Free Software Foundation; either version 2 of the License, or     *
  14.  *   (at your option) any later version.                                   *
  15.  *                                                                         *
  16.  ***************************************************************************/
  17.  
  18. #include "htmlgenerator.h"
  19.  
  20. using namespace std;
  21.  
  22. namespace highlight {
  23.  
  24.  
  25. HtmlGenerator::HtmlGenerator(void)
  26. {}
  27.  
  28. string  HtmlGenerator::formatStyleAttributes(const string & elemName,
  29.                                              const ElementStyle & elem)
  30. {
  31.   ostringstream s;
  32.   s  << "."<<elemName<<"\t{ color:#"
  33.      << (elem.getColour().getHexRedValue())
  34.      << (elem.getColour().getHexGreenValue())
  35.      << (elem.getColour().getHexBlueValue()  )
  36.      << ( elem.isBold() ?"; font-weight:bold" :"" )
  37.      << ( elem.isItalic() ?"; font-style:italic" :"" )
  38.      << ( elem.isUnderline() ?"; text-decoration:underline" :"" )
  39.      << "; }\n" ;
  40.   return  s.str();
  41. }
  42.  
  43. string  HtmlGenerator::getOpenTag(const string& styleName ){
  44.   return "<span class=\""+styleName+"\">";
  45. }
  46.  
  47. HtmlGenerator::HtmlGenerator (
  48.   const string &cssStyle,
  49.   const string &enc,
  50.   bool omitEnc,
  51.   bool withAnchors)
  52.     : CodeGenerator( cssStyle),
  53.     brTag("<br>"),
  54.     hrTag("<hr>"),
  55.     idAttr("name"),
  56.     fileSuffix(".html"),
  57.     encoding(enc),
  58.     omitEncoding(omitEnc),
  59.     HTML_FOOTER(
  60.       "\n</body>\n</html>\n<!--HTML generated by highlight "
  61.       HIGHLIGHT_VERSION
  62.       ", "
  63.       HIGHLIGHT_URL
  64.       "-->\n"),
  65.     attachAnchors(withAnchors)
  66. {
  67.   styleTagOpen.push_back("");
  68.   styleTagOpen.push_back(getOpenTag("str"));
  69.   styleTagOpen.push_back(getOpenTag("num"));
  70.   styleTagOpen.push_back(getOpenTag("slc"));
  71.   styleTagOpen.push_back(getOpenTag("com"));
  72.   styleTagOpen.push_back(getOpenTag("esc"));
  73.   styleTagOpen.push_back(getOpenTag("dir"));
  74.   styleTagOpen.push_back(getOpenTag("dstr"));
  75.   styleTagOpen.push_back(getOpenTag("line"));
  76.   styleTagOpen.push_back(getOpenTag("sym"));
  77.  
  78.   styleTagClose.push_back("");
  79.   for (int i=1;i<NUMBER_BUILTIN_STYLES; i++) {
  80.     styleTagClose.push_back("</span>");
  81.   }
  82.  
  83.   /*assert (styleTagOpen.size()==styleTagClose.size());
  84.   assert (styleTagOpen.size()==NUMBER_BUILTIN_STYLES);
  85. */
  86.   newLineTag = "\n";
  87.   spacer = " ";
  88.   styleCommentOpen="/*";
  89.   styleCommentClose="*/";
  90. }
  91.  
  92. string HtmlGenerator::getStyleDefinition()
  93. {
  94.   if (styleDefinitionCache.empty()){
  95.     ostringstream os;
  96.     os << "body.hl\t{ background-color:#"
  97.        << (docStyle.getBgColour().getHexRedValue())
  98.        << (docStyle.getBgColour().getHexGreenValue())
  99.        << (docStyle.getBgColour().getHexBlueValue())
  100.        << "; }\n";
  101.     os << "pre.hl\t{ color:#"
  102.        << (docStyle.getDefaultStyle().getColour().getHexRedValue())
  103.        << (docStyle.getDefaultStyle().getColour().getHexGreenValue())
  104.        << (docStyle.getDefaultStyle().getColour().getHexBlueValue()  )
  105.        << "; background-color:#"
  106.        << (docStyle.getBgColour().getHexRedValue())
  107.        << (docStyle.getBgColour().getHexGreenValue())
  108.        << (docStyle.getBgColour().getHexBlueValue())
  109.        << "; font-size:"
  110.        << docStyle.getFontSize()
  111.        << "pt; font-family:Courier;}\n";
  112.     os << formatStyleAttributes("num", docStyle.getNumberStyle())
  113.        << formatStyleAttributes("esc", docStyle.getEscapeCharStyle())
  114.        << formatStyleAttributes("str", docStyle.getStringStyle())
  115.        << formatStyleAttributes("dstr", docStyle.getDirectiveStringStyle())
  116.        << formatStyleAttributes("slc", docStyle.getSingleLineCommentStyle())
  117.        << formatStyleAttributes("com", docStyle.getCommentStyle())
  118.        << formatStyleAttributes("dir", docStyle.getDirectiveStyle())
  119.        << formatStyleAttributes("sym", docStyle.getSymbolStyle())
  120.        << formatStyleAttributes("line", docStyle.getLineStyle());
  121.  
  122.     KeywordStyles styles = docStyle.getKeywordStyles();
  123.     for (KSIterator it=styles.begin(); it!=styles.end(); it++){
  124.       os << formatStyleAttributes(it->first, *(it->second));
  125.     }
  126.     styleDefinitionCache=os.str();
  127.   }
  128.   return styleDefinitionCache;
  129. }
  130.  
  131. string HtmlGenerator::getHeader(const string &title)
  132. {
  133.   ostringstream os;
  134.   os << getHeaderStart((title.empty())?"Source file":title );
  135.   if (langInfo.getSyntaxHighlight())
  136.   {
  137.     if (includeStyleDef)    //CSS-Definition in HTML-<head> einfuegen
  138.       {
  139.         os << "<style type=\"text/css\">\n";
  140.         os << "<!--\n";
  141.         os << getStyleDefinition();
  142.         os << CodeGenerator::readUserStyleDef();
  143.         os << "//-->\n";
  144.         os << "</style>" << endl;
  145.       }
  146.     else  //Referenz auf CSS-Datei einfuegen
  147.       {
  148.         os << "<link rel=\"stylesheet\" type=\"text/css\" href=\""
  149.            << getStyleOutputPath()
  150.            << "\""
  151.            << ">\n";
  152.       }
  153.   }
  154.   os << "</head>\n<body class=\"hl\">\n<pre class=\"hl\">";
  155.   return os.str();
  156. }
  157.  
  158. string HtmlGenerator::getFooter()
  159. {
  160.   return "</pre>" + HTML_FOOTER;
  161. }
  162.  
  163.  
  164. void HtmlGenerator::printBody()
  165. {
  166.   processRootState();
  167. }
  168.  
  169.  
  170.  
  171. string HtmlGenerator::maskCharacter(unsigned char c)
  172. {
  173.   switch (c) {
  174.     case '<' :
  175.       return "<";
  176.       break;
  177.     case '>' :
  178.       return ">";
  179.       break;
  180.     case '&' :
  181.       return "&";
  182.       break;
  183.     case '\"' :
  184.       return """;
  185.       break;
  186.  
  187.     case '@' :
  188.       return "@";
  189.       break;
  190.  
  191.     default :
  192.       string m;
  193.       return m += c;
  194.     }
  195. }
  196.  
  197. void HtmlGenerator::insertLineNumber (bool insertNewLine)
  198. {
  199.   if (insertNewLine){
  200.     //*out << getNewLine();
  201.     wsBuffer += getNewLine();
  202.   }
  203.   if (showLineNumbers) {
  204.     ostringstream numberPrefix;
  205.     if (attachAnchors) {
  206.       numberPrefix << "<a "
  207.                    << idAttr
  208.                    << "=\"l_"
  209.                    << lineNumber
  210.                    << "\">";
  211.     }
  212.     ostringstream os;
  213.     if (lineNumberFillZeroes) os.fill('0');
  214.     os <<setw(LINE_NUMBER_WIDTH)<<right<< lineNumber;
  215.     numberPrefix<< styleTagOpen[LINENUMBER]
  216.                 << os.str()
  217.                 << spacer
  218.                 << styleTagClose[LINENUMBER];
  219.  
  220.     if (attachAnchors) {
  221.        numberPrefix << "</a>";
  222.     }
  223.  
  224.     wsBuffer += numberPrefix.str();
  225.   }
  226. }
  227.  
  228. string HtmlGenerator::getHeaderStart(const string &title){
  229.     ostringstream header;
  230.     header<<  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"
  231.             << "\n<html>\n<head>\n";
  232.     if (!omitEncoding){
  233.         header << "<meta http-equiv=\"content-type\" content=\"text/html; charset="<<encoding<<"\">\n";
  234.     }
  235.     header << "<title>" << title <<"</title>\n";
  236.     return header.str();
  237. }
  238.  
  239. bool HtmlGenerator::printIndexFile(const vector<string> &fileList,
  240.                                    const string &outPath ){
  241.   string suffix = fileSuffix;
  242.   string outFilePath = outPath + "index" + suffix;
  243.   ofstream indexfile(outFilePath.c_str());
  244.  
  245.   if (!indexfile.fail()){
  246.      string inFileName;
  247.      string inFilePath, newInFilePath;
  248.      indexfile << getHeaderStart("Source Index" );
  249.      indexfile << "</head>\n<body>\n<h1> Source Index</h1>\n"
  250.                << hrTag
  251.                <<  "\n<ul>\n";
  252.      string::size_type pos;
  253.      for (unsigned int i=0; i < fileList.size();  i++){
  254.          pos=(fileList[i]).find_last_of(Platform::pathSeparator);
  255.          if (pos!=string::npos){
  256.              newInFilePath = (fileList[i]).substr(0, pos+1);
  257.          } else {
  258.             newInFilePath=Platform::pathSeparator;
  259.          }
  260.          if (newInFilePath!=inFilePath){
  261.            indexfile << "</ul>\n<h2>";
  262.            indexfile << newInFilePath;
  263.            indexfile << "</h2>\n<ul>\n";
  264.            inFilePath=newInFilePath;
  265.          }
  266.          inFileName = (fileList[i]).substr(pos+1);
  267.          indexfile << "<li><a href=\"" << inFileName << suffix << "\">";
  268.          indexfile << inFileName << suffix <<"</a></li>\n";
  269.      }
  270.  
  271.      indexfile << "</ul>\n"
  272.                << hrTag << brTag
  273.                << "<small>Generated by highlight "
  274.                << HIGHLIGHT_VERSION
  275.                << ", <a href=\"" << HIGHLIGHT_URL << "\" target=\"new\">"
  276.                << HIGHLIGHT_URL << "</a></small>";
  277.      indexfile << HTML_FOOTER;
  278.   } else {
  279.     return false;
  280.   }
  281.   return true;
  282. }
  283.  
  284. string HtmlGenerator::getMatchingOpenTag(unsigned int styleID){
  285.   return getOpenTag(langInfo.getKeywordClasses()[styleID]);
  286.  }
  287.  
  288. string HtmlGenerator::getMatchingCloseTag(unsigned int styleID){
  289.   return "</span>";
  290. }
  291.  
  292. }
  293.